package com.koron.order.experiment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.experiment.bean.po.ExperimentParamsPo;
import com.koron.order.experiment.mapper.ExperimentParamsMapper;
import com.koron.order.experiment.service.ExperimentParamsService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * (ExperimentParams)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-09 09:48:44
 */
@Service("experimentParamsService")
public class ExperimentParamsServiceImpl implements ExperimentParamsService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, ExperimentParamsPo experimentParams) {

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);

        if (StringUtils.isEmpty(experimentParams.getId())) {
            experimentParams.setId(CodeTools.getCode32());
        }

        return mapper.insertOrUpdate(experimentParams);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<ExperimentParamsPo> experimentParamsList) {

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);

        if (CollectionUtils.isEmpty(experimentParamsList)) {
            return 0;
        }
        experimentParamsList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        return mapper.insertOrUpdateBatch(experimentParamsList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, ExperimentParamsPo experimentParams) {

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);

        return mapper.insert(experimentParams);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<ExperimentParamsPo> experimentParamsList) {

        if (CollectionUtils.isEmpty(experimentParamsList)) {
            return 0;
        }

        experimentParamsList.forEach(p->{
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);
        //先根据试验工单id删除
        mapper.deleteByBusinessId(experimentParamsList.get(0).getExperimentId(),"","");
        return mapper.insertBatch(experimentParamsList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, ExperimentParamsPo experimentParams) {
        Assert.hasText(experimentParams.getId(),"主键id不能为空");
        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);
        return mapper.update(experimentParams);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public ExperimentParamsPo queryById(SessionFactory factory, String id) {

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ExperimentParamsPo> queryList(SessionFactory factory, ExperimentParamsPo experimentParams) {

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);

        List<ExperimentParamsPo> list = mapper.queryList(experimentParams);

        return list;
    }

    @Override
    public List<ExperimentParamsPo> queryByEquipmentId(SessionFactory factory, String orderId, String experimentEquipId) {

        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);

        ExperimentParamsPo experimentParamsPo = new ExperimentParamsPo();
        experimentParamsPo.setExperimentId(orderId);
        experimentParamsPo.setExperimentEquipmentId(experimentEquipId);
        List<ExperimentParamsPo> list = mapper.queryList(experimentParamsPo);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, ExperimentParamsPo experimentParams, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());
        ExperimentParamsMapper mapper = factory.getMapper(ExperimentParamsMapper.class);

        List<ExperimentParamsPo> list = mapper.queryList(experimentParams);

        return new PageInfo(list);
    }

}
